package com.pswv2.backstage.data_management.action;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pswv2.backstage.data_management.servic.DataManagementService;
import com.pswv2.bub.bsc.dao.po.DataManagement;
import com.pswv2.util.CommonResp;
import com.pswv2.util.ExtraSpringHibernateTemplate;
/**
 * 分类操作
 * @author linjiang
 *
 */
@Controller
@SuppressWarnings("unchecked")
@RequestMapping(value = "dataManagementAction", produces = { "text/json;charset=UTF-8" })
public class DataManagementAction {
	private static Class<?> poc = DataManagement.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private DataManagementService dataManagementService;

	/**
	 * 修改发布信息获取的积分
	 */
	@ResponseBody
	@RequestMapping(value = { "editDataManagement" })
	public String editDataManagement(Integer releaseInformationPoints,double releaseInformationPrice) {
		List<DataManagement> ls=(List<DataManagement>) dataManagementService.queryAll();
		DataManagement entity=ls.get(0);
		entity.setReleaseInformationPoints(releaseInformationPoints);
		entity.setReleaseInformationPrice(releaseInformationPrice);
		dataManagementService.updata(entity);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 修改发布信息获取的积分
	 */
	@ResponseBody
	@RequestMapping(value = { "editReleaseInformationPoints" })
	public String editReleaseInformationPoints(Integer releaseInformationPoints) {
		List<DataManagement> ls=(List<DataManagement>) dataManagementService.queryAll();
		DataManagement entity=ls.get(0);
		entity.setReleaseInformationPoints(releaseInformationPoints);
		dataManagementService.updata(entity);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 控制需求发布条数
	 */
	@ResponseBody
	@RequestMapping(value = { "editManageReleaseInformationPrice" })
	public String editManageReleaseInformationPrice(double releaseInformationPrice) {
		List<DataManagement> ls=(List<DataManagement>) extraSpringHibernateTemplate.findAll(poc);
		List<?> money=extraSpringHibernateTemplate.getHibernateTemplate().find("select SUM(money) from MyWallet");
		if(ls.size()==0){
			DataManagement dm=new DataManagement();
			dm.setReleaseInformationPrice(releaseInformationPrice);
			extraSpringHibernateTemplate.getHibernateTemplate().save(dm);
			return CommonResp.buildSuccessResp(dm).toJsonString();
		}else{
			DataManagement dm=ls.get(0);
			dm.setReleaseInformationPrice(releaseInformationPrice);
			extraSpringHibernateTemplate.getHibernateTemplate().update(dm);
			return CommonResp.buildSuccessResp(dm).toJsonString();
		}
	}
	/**
	 * 控制需求发布条数
	 */
	@ResponseBody
	@RequestMapping(value = { "queryAllFund" })
	public String queryAllFund() {
		List<DataManagement> ls=(List<DataManagement>) extraSpringHibernateTemplate.findAll(poc);
		List<?> money=extraSpringHibernateTemplate.getHibernateTemplate().find("select SUM(money) from MyWallet");
		if(ls.size()==0){
			DataManagement dm=new DataManagement();
			dm.setAllFund(Double.parseDouble(money.get(0).toString()));
			extraSpringHibernateTemplate.getHibernateTemplate().save(dm);
			return CommonResp.buildSuccessResp(dm).toJsonString();
		}else{
			DataManagement dm=ls.get(0);
			dm.setAllFund(Double.parseDouble(money.get(0).toString()));
			extraSpringHibernateTemplate.getHibernateTemplate().update(dm);
			return CommonResp.buildSuccessResp(dm).toJsonString();
		}
	}
	/**
	 * 控制需求发布条数
	 */
	@ResponseBody
	@RequestMapping(value = { "editRquirementReleaseAmount" })
	public String editRquirementReleaseAmount(Integer amount,double easyPrice,double mediumPrice,
			double complexPrice,Integer initialRowUsage) {
		List<DataManagement> ls=(List<DataManagement>) extraSpringHibernateTemplate.findAll(poc);
		System.out.println(initialRowUsage);
		if(ls.size()==0){
			DataManagement dm=new DataManagement();
			dm.setRequirementReleaseAmount(amount);
			dm.setEasyPrice(easyPrice);
			dm.setMediumPrice(mediumPrice);
			dm.setComplexPrice(complexPrice);
			dm.setInitialRowUsage(initialRowUsage);
			extraSpringHibernateTemplate.getHibernateTemplate().save(dm);
		}else{
			DataManagement dm=ls.get(0);
			dm.setRequirementReleaseAmount(amount);
			dm.setRequirementReleaseAmount(amount);
			dm.setEasyPrice(easyPrice);
			dm.setMediumPrice(mediumPrice);
			dm.setComplexPrice(complexPrice);
			dm.setInitialRowUsage(initialRowUsage);
			extraSpringHibernateTemplate.getHibernateTemplate().update(dm);
		}
		return CommonResp.buildSuccessResp().toJsonString();
	}
	
	
	/**
	 * 查询控制全部信息
	 */
	@ResponseBody
	@RequestMapping(value = { "query" })
	public String query() {
		List<DataManagement> ls=(List<DataManagement>) extraSpringHibernateTemplate.findAll(poc);
		if(ls.size()==0){
			return CommonResp.buildSuccessResp().toJsonString();
		}else{
			DataManagement dm=ls.get(0);
			return CommonResp.buildSuccessResp(dm).toJsonString();
		}
	}
}
